import Int from "../type/Q/int"
import isPrime from "./is-prime"

/**
 * 
 * @param {BigInt} n 
 * @return {BigInt[]}
 */
function primeFactorization(n) {
    if (n == 1n)
        return []
    if (n in Int.primeFractors)
        return Int.primeFractors[n]
    /**
     * @type {BigInt[]}
     */
    const result = []
    /**
     * @type {BigInt}
     */
    let prime = 2n
    let temp = n
    while (! isPrime(temp)) {
        if (!(temp % prime)) {
            result.push(prime)
            temp /= prime
        } else {
            while (! isPrime(++prime))
                ;
        }
    }
    result.push(temp)
    Int.setPrimeFractor(n.toString(), result)
    return result
}

export default primeFactorization